﻿using TrueSync;

namespace HEFramework
{
    public static class SkillPreparationComponentSystem
    {
        [EntitySystem]
        public class SkillPreparationComponentAwakeSystem : AwakeSystem<SkillPreparationComponent>
        {
            protected override void Awake(SkillPreparationComponent _self)
            {
            }
        }


        public static int Ready(this SkillPreparationComponent _self, int _id, Entity _receiver, TSVector _pos, bool _isActive)
        {
            int id = _id;
            var sc = _self.GetParent<SkillComponent>();
            if (sc == null || !sc.SkillIDs.Contains(id))
            {
                return (int)eSkillErrorCode.System;
            }

            var tc = sc.GetParentChild<TransformComponent>();
            if (tc == null)
            {
                return (int)eSkillErrorCode.System;
            }

            var scl = sc.GetChild<SkillLockComponent>();
            var scc = sc.GetChild<SkillCooldownComponent>();
            var src = sc.GetChild<SkillRangeComponent>();
            var src1 = sc.GetChild<SkillResourceComponent>();
            var sec = sc.GetChild<SkillEffectComponent>();
            var setc = sec.GetChild<SkillEffectTargetComponent>();

            int code = scl.Ready(id);
            if (code != (int)eSkillErrorCode.None)
            {
                return code;
            }

            code = scc.Ready(id, _isActive);
            if (code != (int)eSkillErrorCode.None)
            {
                return code;
            }

            code = src.Ready(id, tc.P, _pos, _isActive);
            if (code != (int)eSkillErrorCode.None)
            {
                return code;
            }

            code = src1.Ready(id, _isActive);
            if (code != (int)eSkillErrorCode.None)
            {
                return code;
            }

            code = setc.Ready(id, _receiver, _pos, _isActive);
            if (code != (int)eSkillErrorCode.None)
            {
                return code;
            }


            return (int)eSkillErrorCode.None;
        }
    }
}